package com.fr.chicken.controller.scheduletask;


import com.fr.chicken.dao.*;
import com.fr.chicken.manager.configure.ConfigManager;
import com.fr.chicken.model.*;
import com.fr.chicken.model.vo.ChickenList;
import com.fr.chicken.model.vo.ChickenTimeList;
import com.fr.chicken.model.vo.HouseTimeList;
import com.fr.chicken.util.DateUtil;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
@EnableScheduling
public class ChickenScheduleTask {

    @Autowired
    private ChickenInfoDao chickenInfoDao;
    @Autowired
    private HouseInfoDao houseInfoDao;
    @Autowired
    private ChickenConfigureDao chickenConfigureDao;
    @Autowired
    private MessageLogDao messageLogDao;
    @Autowired
    private ConfigManager configManager;
    @Autowired
    private ActivationLogDao activationLogDao;
    //生成一个鸡蛋
    @Transactional
    public void generateEgg(){
        ChickenConfigure chickenConfigure = chickenConfigureDao.selectByPrimaryKey(3);
        List<ChickenInfo> chickenInfos = chickenInfoDao.selectAll();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Long nowDate = System.currentTimeMillis() / 1000;
        Calendar now = Calendar.getInstance();
        now.setTime(new Date());
        now.set(Calendar.HOUR, now.get(Calendar.HOUR)-chickenConfigure.getValue());
        String date = sdf.format(now.getTime());
        List<ChickenList> chickenLists = new ArrayList<>();
        List<HouseInfo> houseInfos = houseInfoDao.selectAll();
        ArrayList<Integer> chickenIds = new ArrayList<>();
        ArrayList<ChickenTimeList> chickenTimeLists = new ArrayList<>();
        for (HouseInfo x1: houseInfos){
            Integer eggNum = 0;
        for (ChickenInfo x:chickenInfos){
            if (x.getHouseId().intValue()==x1.getId().intValue()){
                String lastLayEggsTime = DateUtil.timeStampDateString(x.getLastLayEggsTime().toString(),"yyyy-MM-dd HH:mm");
                Integer nums = (nowDate.intValue()-x.getLastLayEggsTime().intValue())/(chickenConfigure.getValue()*3600);
                Integer rtime = (nowDate.intValue()-x.getLastLayEggsTime().intValue())%(chickenConfigure.getValue()*3600);
                if (nums>0&&rtime>120) {
                    ChickenTimeList chickenTimeList = new ChickenTimeList();
                    chickenTimeList.setChickenId(x.getId());
                    chickenTimeList.setTime(nowDate - rtime);
                    chickenTimeLists.add(chickenTimeList);
                    for(int i=0;i<nums;i++) {
                        String random = RandomStringUtils.randomNumeric(1,3);
                        if (Integer.valueOf(random).intValue() < chickenConfigure.getProbability() && chickenConfigure.getProbability() != 0) {
                            eggNum = eggNum + 1;
                            chickenIds.add(x.getId());
                        }
                    }
                }
                if (date.equals(lastLayEggsTime)){
                    String random = RandomStringUtils.randomNumeric(1,3);
                    if (Integer.valueOf(random).intValue() < chickenConfigure.getProbability() && chickenConfigure.getProbability() != 0) {
                        eggNum =eggNum+1;
                        chickenIds.add(x.getId());
                    }
                }
            }
        }
        if (eggNum!=0) {
            ChickenList chickenList = new ChickenList();
            chickenList.setNum(eggNum);
            chickenList.setHouseId(x1.getId());
            chickenLists.add(chickenList);
        }
        }
        if (chickenLists!=null&&chickenLists.size()>0) {
            int num = houseInfoDao.updateEgg(chickenLists);
            if (num < 1) {
                throw new RuntimeException("add egg error");
            }
            int num1 = chickenInfoDao.updateLastTime(chickenIds, nowDate);
            if (num1 < 1) {
                throw new RuntimeException("add egg error");
            }
        }
        if (chickenTimeLists!=null&&chickenTimeLists.size()>0){
            int num2 = chickenInfoDao.updateEggLastTimeByIds(chickenTimeLists);
            if (num2 < 1) {
                throw new RuntimeException("add egg error");
            }
        }
    }
    //鸡死亡
    @Transactional
    public void chickenDie(){
        List<ChickenInfo> chickenInfos = chickenInfoDao.selectAll();
        Long nowDate = System.currentTimeMillis() / 1000;
        ArrayList<Integer> chickenIds = new ArrayList<>();
        for (ChickenInfo x:chickenInfos){
            Long dieDate = x.getBirthday()+(86400*configManager.getIntConfig(5));
            if (dieDate<=nowDate){
                chickenIds.add(x.getId());
            }
        }
        if (chickenIds!=null&&chickenIds.size()>0) {
            int num = chickenInfoDao.deleteByChickenByIds(chickenIds);
            if (num < 1) {
                throw new RuntimeException("delete chicken error");
            }
        }
    }
    //生成一个粪便
    @Transactional
    public void generateFaeces(){
        List<HouseInfo> houseInfos = houseInfoDao.selectAll();
        ChickenConfigure chickenConfigure = chickenConfigureDao.selectByPrimaryKey(1);
        ArrayList<Integer> houseIds = new ArrayList<>();
        ArrayList<Integer> houseIds1 = new ArrayList<>();
        List<HouseTimeList> houseTimeLists = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Long nowDate = System.currentTimeMillis() / 1000;
        Calendar now = Calendar.getInstance();
        now.setTime(new Date());
        now.set(Calendar.HOUR, now.get(Calendar.HOUR)-chickenConfigure.getValue());
        String date = sdf.format(now.getTime());
        for (HouseInfo x:houseInfos) {
            String lastFaecesTime = DateUtil.timeStampDateString(x.getLastFaecesTime().toString(),"yyyy-MM-dd HH:mm");
            Integer nums = (nowDate.intValue()-x.getLastFaecesTime().intValue())/(chickenConfigure.getValue()*3600);
            Integer rtime = (nowDate.intValue()-x.getLastFaecesTime().intValue())%(chickenConfigure.getValue()*3600);
            if (nums>0&&rtime>120) {
                Integer faecesNum = 0;
                for(int i=0;i<nums;i++) {
                    String random = RandomStringUtils.randomNumeric(1,3);
                    if (x.getFaeces()+faecesNum<configManager.getIntConfig(1)) {
                        if (Integer.valueOf(random).intValue() < chickenConfigure.getProbability() && chickenConfigure.getProbability() != 0) {
                            faecesNum=faecesNum+1;
                        }
                    }
                }
                HouseTimeList houseTimeList = new HouseTimeList();
                houseTimeList.setHouseId(x.getId());
                houseTimeList.setNum(faecesNum);
                houseTimeList.setTime(nowDate-rtime);
                houseTimeLists.add(houseTimeList);
            }
            if (date.equals(lastFaecesTime)) {
                houseIds.add(x.getId());
                if (x.getFaeces() < configManager.getIntConfig(1)) {
                    String random = RandomStringUtils.randomNumeric(1,3);
                    if (Integer.valueOf(random).intValue()<chickenConfigure.getProbability()&&chickenConfigure.getProbability()!=0) {
                        houseIds1.add(x.getId());
                    }
                }
            }
        }
        if (houseIds!=null&&houseIds.size()>0) {
            int num1 = houseInfoDao.updateFaecesLastTime(houseIds, nowDate);
            if (num1 < 1) {
                throw new RuntimeException("add faeces error");
            }
        }
        if (houseIds1!=null&&houseIds1.size()>0) {
            int num = houseInfoDao.updateFaeces(houseIds1);
            if (num < 1) {
                throw new RuntimeException("add faeces error");
            }
        }
        if (houseTimeLists!=null&&houseTimeLists.size()>0){
            int num2 = houseInfoDao.updateFaecesByIds(houseTimeLists);
            if (num2 < 1) {
                throw new RuntimeException("add faeces error");
            }
        }
    }
    //刷新鸡毛
    @Transactional
    public void generateFeather(){
        ChickenConfigure chickenConfigure = chickenConfigureDao.selectByPrimaryKey(2);
        int maxConfigure = configManager.getIntConfig(2);
        List<HouseInfo> houseInfos = houseInfoDao.selectAll();
        List<HouseTimeList> houseTimeLists = new ArrayList<>();
        List<HouseTimeList> houseTimeLists1 = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Long nowDate = System.currentTimeMillis() / 1000;
        Calendar now = Calendar.getInstance();
        now.setTime(new Date());
        now.set(Calendar.HOUR, now.get(Calendar.HOUR)-chickenConfigure.getValue());
        String date = sdf.format(now.getTime());
        for (HouseInfo x:houseInfos) {
            ActivationLog activationLog = activationLogDao.selectByInviteeId(x.getPlayerId());
            if (activationLog!=null) {
                String lastFeatherTime = DateUtil.timeStampDateString(x.getLastFeatherTime().toString(), "yyyy-MM-dd HH:mm");
                Integer nums = (nowDate.intValue() - x.getLastFeatherTime().intValue()) / (chickenConfigure.getValue() * 3600);
                Integer rtime = (nowDate.intValue() - x.getLastFeatherTime().intValue()) % (chickenConfigure.getValue() * 3600);
                if (nums > 0 && rtime > 120) {
                    Integer featherNum = 0;
                    Integer featherMax = x.getFeatherMax();
                    for (int i = 0; i < nums; i++) {
                        String random = RandomStringUtils.randomNumeric(1, 3);
                        if (featherMax < maxConfigure) {
                            if (Integer.valueOf(random).intValue() < chickenConfigure.getProbability() && chickenConfigure.getProbability() != 0) {
                                List<ChickenInfo> chickenInfos1 = chickenInfoDao.selectByHouseId(x.getId());
                                featherNum = featherNum + chickenInfos1.size();
                                featherMax = featherMax + 1;
                            }
                        }
                    }
                    HouseTimeList houseTimeList = new HouseTimeList();
                    houseTimeList.setHouseId(x.getId());
                    houseTimeList.setNum(featherNum);
                    houseTimeList.setTime(nowDate - rtime);
                    houseTimeLists.add(houseTimeList);
                }
                if (date.equals(lastFeatherTime)) {
                    if (x.getFeatherMax() < maxConfigure) {
                        String random = RandomStringUtils.randomNumeric(1, 3);
                        if (Integer.valueOf(random).intValue() < chickenConfigure.getProbability() && chickenConfigure.getProbability() != 0) {
                            List<ChickenInfo> chickenInfos = chickenInfoDao.selectByHouseId(x.getId());
                            HouseTimeList houseTimeList1 = new HouseTimeList();
                            houseTimeList1.setHouseId(x.getId());
                            houseTimeList1.setNum(chickenInfos.size());
                            houseTimeList1.setTime(nowDate);
                            houseTimeLists1.add(houseTimeList1);
                        }
                    }
                }
            }
        }
        if (houseTimeLists1 != null && houseTimeLists1.size() > 0) {
            int num = houseInfoDao.updateFeatherByIds(houseTimeLists1);
            if (num < 1) {
                throw new RuntimeException("add feather error");
            }
        }
        if (houseTimeLists != null && houseTimeLists.size() > 0) {
            int num2 = houseInfoDao.updateFeatherByIds(houseTimeLists);
            if (num2 < 1) {
                throw new RuntimeException("add faeces error");
            }
        }
    }
    //删除时间超过两天的时间
    public void deleteMessage(){
        List<MessageLog> messageLogs = messageLogDao.selectAll();
        ArrayList<Integer> list = new ArrayList();
        Long nowDate = System.currentTimeMillis() / 1000;
        for (MessageLog x:messageLogs){
            Long deleteDate = x.getSendTime()+(86400*configManager.getIntConfig(7));
           if (deleteDate<=nowDate){
                list.add(x.getId());
           }
        }
        if (list!=null&&list.size()>0) {
            int num = messageLogDao.deleteByIds(list);
            if (num < 1) {
                throw new RuntimeException("delete message error");
            }
        }
    }
}
